package _5tree;


/**
 * @author Gong Cheng
 * @date 2021/1/14 9:58 上午
 **/
public class pathSum112 {
    public class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x){ val = x;}
    }

    /**
     * 使用递归深度遍历解题
     * @param root
     * @param sum
     * @return
     */
    public boolean hasPathSum(TreeNode root, int sum){
        if (root == null){
            return false;
        }
        int newSum = sum - root.val;

        if (root.left == null && root.right == null) {
            return newSum == 0;
        }
        return hasPathSum(root.left, newSum) || hasPathSum(root.right, newSum);
    }
}
